<?php

    ##################################################
    #
    # Copyright (c) 2008 - 2010 ABSoft Company
    # Written and Designed by Dang Tin Trung
    #
    # This file is part of ABSoft Framework
    #
    ##################################################

    /**
    * Pray Online User Form
    * @author: trungdt
    * @since: Feb 22, 2011
    * 
    * Database structure
    * CREATE TABLE `ag`.`ag_pray_register_ring` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `user_id` INT NOT NULL DEFAULT '0',
    `ring_id` INT NOT NULL DEFAULT '0'
    ) ENGINE = MYISAM ;
    */
    if (!defined('EXPONENT')) exit('');

    require_once(BASE."subsystems/forms/controls/formcontrol.php");

    class prayonline_userform extends formcontrol {
        function name() { return "PrayOnline User Registration form"; }
        function isSimpleControl() { return true; }
        function getFieldDefinition() {
            return array(
            DB_FIELD_TYPE=>DB_DEF_STRING,
            DB_FIELD_LEN=>250,
            );
        }

        function prayonline_userform($user_id=-1) {
            $this->user_id=intval($user_id); // =-1 mean create
            if ($user_id==-1){
                $this->user_id=exponent_sessions_getTicketString();
            }
        }

        function onRegister(&$form) {
            $form->addScript("js-prayonline", PATH_RELATIVE."modules/prayonline_createuserformmodule/prayonline_createuser.js");
        }

        function controlToHTML($name) {
            $html='';
            global $db, $user;
            
            $time_belt=$user->time_belt;
            
            $user_id=$user->id;
            $regis=$db->selectObjectsBySql("
            SELECT {$db->prefix}pray_register.* 
            FROM {$db->prefix}pray_register 
            WHERE user_id = {$user_id}
            ORDER BY fhour, fmin, thour, tmin
            ");
            foreach ($regis as $key=>$value)
            {
                // convert to user local time
                $value->fhour_local=0;
                $value->thour_local=0;
                $value->fmin_local=0;
                $value->tmin_local=0;
                aboid_localtime_convert($value->fhour,$value->fmin, $value->fhour_local, $value->fmin_local);
                aboid_localtime_convert($value->thour,$value->tmin, $value->thour_local, $value->tmin_local);
                // pretty time make
                $value->fhour=str_pad($value->fhour,2,"0",STR_PAD_LEFT);
                $value->fmin=str_pad($value->fmin,2,"0",STR_PAD_LEFT);
                $value->thour=str_pad($value->thour,2,"0",STR_PAD_LEFT);
                $value->tmin=str_pad($value->tmin,2,"0",STR_PAD_LEFT);
                // 
                $value->fhour_local=str_pad($value->fhour_local,2,"0",STR_PAD_LEFT);
                $value->fmin_local=str_pad($value->fmin_local,2,"0",STR_PAD_LEFT);
                $value->thour_local=str_pad($value->thour_local,2,"0",STR_PAD_LEFT);
                $value->tmin_local=str_pad($value->tmin_local,2,"0",STR_PAD_LEFT);
            }
            
            // get pray ring
            $rings=$db->selectObjects("pray_ring");
            $rings_register=$db->selectObjects("pray_register_ring","user_id = {$user_id}");
            foreach ($rings as $k=>$v)
            {
                foreach ($rings_register as $kr=>$vr)
                {
                    if ($vr->ring_id == $v->id)
                    {
                        $v->checked = true;
                        $vr->name=$v->name;
                    }
                }
            }

            $hour_options="";
            $min_option="";
            for ($i=0;$i<24;$i++)
            {
                $hour_options.="<option value='{$i}'>{$i}</option>";
            }
            for ($i=0;$i<4;$i++)
            {
                $j=$i*15;
                $min_option.="<option value='{$j}'>{$j}</option>";
            }
            
            $list_box=new listboxcontrol($rings,true);
            $listbox_html=$list_box->controlToHTML("subject");
            
            if ($time_belt>0){
                $time_belt="+".$time_belt;
            }
            
            $template=new template("prayonline_createuserformmodule","_register_form");

            $template->assign("name",$name);
            $template->assign("rings",$rings);
            $template->assign("rings_register",$rings_register);
            $template->assign("subject_html",$listbox_html);
            $template->assign("regis",$regis);
            $template->assign("hour_options",$hour_options);
            $template->assign("time_belt",$time_belt);
            $template->assign("server_belt","+7");
            $template->assign("min_option",$min_option);

            $html=$template->render();                
            return $html;
        }

        function form($object) {
            if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
            exponent_forms_initialize();

            $form = new form();
            if (!isset($object->identifier)) {
                $object->identifier = "";
                $object->caption = "";
                $object->default = "";
            }
            $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

            $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
            $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
            $form->register("default",$i18n['default'], new textcontrol($object->default));
            $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
            return $form;
        }

        function update($values, $object) {
            if ($object == null) $object = new uploadcontrol();
            if ($values['identifier'] == "") {
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                $post = $_POST;
                $post['_formError'] = $i18n['id_req'];
                exponent_sessions_set("last_POST",$post);
                return null;
            }
            $object->identifier = $values['identifier'];
            $object->caption = $values['caption'];
            $object->default = $values['default'];
            return $object;
        }

        function parseData($original_name,$formvalues) {
            $file = $formvalues[$original_name];
            return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
        }
    }

?>
